package com.kq.xtoolkit.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.kq.xtoolkit.R;
import com.kq.xtoolkit.XToolKitService;
import com.kq.xtoolkit.utils.XUtils;

public class BallView extends ImageView implements OnGestureListener{
    private Context mContext;
    public static final int HALF_TRANSPARANT = 60;
    private boolean isHalfTransparent;

    private Drawable ballDrawable;
    private Handler mServiceHandler;
    private GestureDetector detector;
    public enum State {
        IDLE, ACTIVE
    }
    private State mState;
    public BallView(Context context) {
        this(context, null);

    }
    public BallView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        ballDrawable = getResources().getDrawable(R.drawable.ball);
        setImageDrawable(ballDrawable);
        detector = new GestureDetector(context, this);
    }

    private void setState(State state) {
        mState = state;
    }

    public State getState() {
        return mState;
    }
    public void setAlphaValue(int alpha) {
        if (ballDrawable == null) {
            return;
        }
        ballDrawable.setAlpha(alpha);
        invalidate();
        if (alpha == HALF_TRANSPARANT) {
            isHalfTransparent = true;
        } else {
            isHalfTransparent = false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                setState(State.ACTIVE);
                break;
            case MotionEvent.ACTION_UP:
                setState(State.IDLE);
                if (!isHalfTransparent) {
                    mServiceHandler.sendEmptyMessageDelayed(XToolKitService.MSG_SET_BALL_HALF_TRANSPARENT, 3000);
                }
                break;
        }
        return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        setAlphaValue(255);
        return true;
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        XUtils.LogD("==onFling== velocityX:"+velocityX+" velocityY:"+velocityY);
        if (velocityX > 2500 && mServiceHandler != null) {
            Bundle bundle = new Bundle();
            bundle.putFloat("velocityX", velocityX);
            bundle.putFloat("velocityY", velocityY);
            Message msg = mServiceHandler.obtainMessage();
            msg.setData(bundle);
            msg.what = XToolKitService.MSG_FLING;
            mServiceHandler.sendMessage(msg);
            return true;
        }
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {
        XUtils.vibrate(mContext, 20);
        mServiceHandler.obtainMessage(XToolKitService.MSG_START_SETTING_ACTIVITY).sendToTarget();
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        XUtils.LogD("==onScroll== distaceX:"+distanceX);
        if ((distanceX*distanceX+distanceY*distanceY) > 16) {
            float newX = e2.getRawX();
            float newY = e2.getRawY();
            updatePosition(newX, newY);
            return true;
        }
        return false;
    }
    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        if (mServiceHandler != null) {
            mServiceHandler.obtainMessage(XToolKitService.MSG_LIGHT_FLASH).sendToTarget();
            return true;
        }
        return false;
    }

    private void updatePosition(float x, float y) {
        if (mServiceHandler != null) {
            Message msg = new Message();
            msg.what = XToolKitService.MSG_UPDATE_BALL_POSITION;
            Bundle bundle = new Bundle();
            bundle.putFloat("x", x);
            bundle.putFloat("y", y);
            msg.setData(bundle);
            mServiceHandler.sendMessage(msg);
        }
    }

    public void setServiceHandler(Handler handler) {
        mServiceHandler = handler;
    }

}
